'==========================================================================
'Subject: CueCat Barcode Decodeing Routines     Date: 05-01-03
' Author: Robert F. Gage II                     Code: PB
'  EMail: BGage@BigFoot.com                File Name: CueCat.bi
'WebPage: http://Bobby.MyDis.org           Copyright: Public Domain
'--------------------------------------------------------------------------
'I would like to give thanks to Ed Turner for the use of the sorce code to
'this program.  It is his base sorce code (Base 64 Encodeing) that I
'modified to work with the CueCat barcode reader.
'--------------------------------------------------------------------------
'Explination of the main Routine.
'
'CueCat ccText$,ccSN$,ccType$,ccBar$
'
'       ccText$   - Is the string that is returned by the CueCat scanner.
'       ccSN$     - Returns the S/N of the CueCat
'       ccType$   - Returns the barcode type that was scanned
'       ccBar$    - Returns the barcode data
'
'The differenet types of barcodes that can be returned in the ccTypes$ is
'determined on what the CueCat can read.  I know of some of them but not 
'all.  The following is a list of what I do know of:
'
'       UPA - UPC type A
'       UA2 - UPS type A 2-Digit Supplement
'       UA5 - UPC type A 5-Digit Supplement
'       UPE - UPC type E
'       UE2 - UPC type E 2-Digit Supplement
'       UE5 - UPC type E 5-Digit Supplement
'       C39 - Code 39 (Barcode 3 of 9)
'       C93 - Code 93
'       128 - Code 128
'       CBR - Codabar
'       PLS - MSI/Plessey
'       E08 - EAN/JAN 8
'       E82 - EAN/JAN 8 2-Digit Supplement
'       E85 - EAN/JAN 8 5-Digit Supplement
'       E13 - EAN/JAN 13
'       E32 - EAN/JAN 13 2-Digit Supplement
'       E35 - EAN/JAN 13 5-Digit Supplement
'       IB5 - ISBN + 5
'       IB2 - ISBN + 2
'       IBN - ISBN
'       ITF - Interleaved 2 of 5
'       CC! - CueCat Slant Code (the Barcode type that is used in catalogs.)
'             You must use the SlantCode Routine to decode the SlantCode
'             data to slant code numbers.
'==========================================================================

'Call using the text from the cuecat as ccCueCat$ then the CueCat routine
'return the Serial number in ccSN$ and the barcode type in ccType$ and
'the barcode in ccBar$

SUB CueCat(ccCueCat$,ccSN$,ccType$,ccBar$)
	ccCueCat$=ccCueCat$+"...."
	ccDevide ccCueCat$,ccSN$,ccType$,ccBar$
	ccSN$=ccDecode$(ccSN$)
	ccType$=ccDecode$(ccType$)
	ccBar$=ccDecode$(ccBar$)	
END SUB

'ccDevide will device the cuecat text (ccCueCat$) into it's three text parts
'and return them as still encoded strings as ccSN$, ccType$, and ccBar$

SUB ccDevide(ccCueCat$,ccSN$,ccType$,ccBar$)
For SectionType%=0 to 3
	DataType$=""
	Do
	TestChar%=TestChar%+1
		dummy$=Mid$(ccCueCat$,TestChar%,1)
		IF dummy$<>Chr$(0) Then
			IF dummy$="." Then
               	Select Case SectionType%
                    	Case 1
                         	ccSN$=DataType$
                         Case 2
                         	ccType$=DataType$
                         Case 3
                         	ccBar$=DataType$
                    End Select
                    Exit Loop
               End If
     		DataType$=DataType$+dummy$
		End If
	Loop
Next
End SUB

'Internal function used by ccDecode

Function ccConvertToNvt(t$) as byte
	S$ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-"
	Function=instr(s$,t$)-1
end Function

'Will take the CueCat encoded text and return the correct text as a string.
'Will not work with a string directly out of the cuecat.  The string must
'first be devided up using the ccDevide routine and then each encoded part
'may be passed through the ccDecode function.

FUNCTION ccDecode(BYVAL strng AS STRING) AS STRING
	strng=strng+"="
       n& = Len(strng)
       For i& = 1 to n& step 4
	       Firstletter$ = Mid$(strng, i&, 1)
	       Secondletter$ = Mid$(strng, i& + 1, 1)
	       Thirdletter$ = Mid$(strng, i& + 2, 1)
	       Fourthletter$ = Mid$(strng, i& + 3, 1)
	       IF FirstLetter$="=" then exit for
	       first? = ccConvertToNvt(Firstletter$)
	       SHIFT LEFT first?, 2
	       temp? = ccConvertToNvt(Secondletter$)
	       SHIFT RIGHT temp?, 4
	       first? = (( first? OR temp? ) XOR 3 )+64
	       IF first? > 128 then first?=first?-128
	       If Thirdletter$ = "=" Then
		       rtn$ = rtn$ & Chr$(first?)
	       Else
		       second? = ccConvertToNvt(Secondletter$)
		       SHIFT LEFT second?, 4
		       temp? = ccConvertToNvt(Thirdletter$)
		       SHIFT RIGHT temp?, 2
		       second? = (( second? OR temp? ) XOR 3 )+64
		       if second? > 128 then second?=second?-128
		       If Fourthletter$ = "=" Then
			       rtn$ = rtn$ & Chr$(first?) & Chr$(second?)
		       Else
			       third? = ccConvertToNvt(Thirdletter$)
			       SHIFT LEFT third?, 6
			       temp? = ccConvertToNvt(Fourthletter$)
			       third? = ( third? OR temp? ) XOR 3 +64
			       rtn$ = rtn$ & Chr$(first?) & Chr$(second?) & Chr$(third?)
		       End If
	       End If
       Next i&
       FUNCTION = rtn$
END FUNCTION

'This will take an already decoded string and convert it to a slant code
'string.   Should only be used if the barcode type is "CC!".
'I'm not fully shure of how this Function will work with all slant codes so
'let me know if you encounter a problem with it.

Function SlantCode$(ccText$)
	For A=1 to Len(ccText$)
		dummy%=Asc(Mid$(ccText$,A,1))-32
		if dummy%>9 then
			ccTemp$=ccTemp$+Trim$(Str$(dummy%))+" "
		else
			ccTemp$=ccTemp$+"0"+Trim$(Str$(dummy%))+" "
		End if
	Next
	SlantCode$=ccTemp$
End Function

